<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="80px">
    <el-form-item :label="$t('layout.personal.basic.nickname')" prop="nickName">
      <el-input v-model="user.nickName" maxlength="30" />
    </el-form-item>
    <el-form-item :label="$t('layout.personal.basic.phone')" prop="phonenumber">
      <el-input v-model="user.phonenumber" maxlength="11" />
    </el-form-item>
    <el-form-item :label="$t('layout.personal.basic.email')" prop="email">
      <el-input v-model="user.email" maxlength="50" />
    </el-form-item>
    <el-form-item :label="$t('layout.personal.basic.sex')">
      <el-radio-group v-model="user.sex">
        <el-radio label="0">{{$t('layout.personal.basic.male')}}</el-radio>
        <el-radio label="1">{{$t('layout.personal.basic.female')}}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item :label="$t('layout.personal.basic.language')">
      <el-radio-group v-model="user.language">
        <el-radio label="zh_CN">{{$t('layout.personal.basic.chinese')}}</el-radio>
        <el-radio label="en_US">{{$t('layout.personal.basic.english')}}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item :label="$t('layout.personal.basic.mapType')">
      <el-radio-group v-model="user.mapType" @change="handleMapChange">
        <el-radio label="1">{{$t('layout.personal.basic.google')}}</el-radio>
        <el-radio label="2">{{$t('layout.personal.basic.amap')}}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">{{$t('common.button.save')}}</el-button>
      <el-button type="danger" size="mini" @click="close">{{$t('common.button.close')}}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserProfile } from "@/api/system/user";
import { setLanguage,setMapType } from '@/utils/auth'

export default {
  props: {
    user: {
      type: Object
    }
  },
  data() {
    return {
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: this.$t('layout.personal.basic.rules.nickname.required'), trigger: "blur" }
        ],
        email: [
          { required: true, message: this.$t('layout.personal.basic.rules.email.required'), trigger: "blur" },
          { type: "email", message: this.$t('layout.personal.basic.rules.email.pattern'), trigger: ["blur", "change"]}
        ],
        phonenumber: [
          { required: true, message: this.$t('layout.personal.basic.rules.phone.required'), trigger: "blur" },
          { pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: this.$t('layout.personal.basic.rules.phone.pattern'), trigger: "blur"}
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserProfile(this.user).then(response => {
            setLanguage(this.user.language);
            setMapType(this.user.mapType)
            this.$modal.updateSuccess();
            window.location.reload();
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    },
    handleMapChange(e){
      console.log(e);
    }
  }
};
</script>
